﻿using Gateway.IApplication.UserMessage;
using Gateway.Infrastructure.Provider.Operator;
using Aurora.SingalR.Hubs;
using Microsoft.AspNetCore.SignalR;

namespace Gateway.UI.Api.Hubs
{
    /// <summary>
    /// 消息
    /// </summary>
    [HubRoute("/api/signalr")]
    public class MessageHub : Hub
    {
        // : Hub<IMessageHub>
        private const string GROUP_NAME = "GATEWAY_GROUP";
        private readonly IUserMessageApp _userMessageApp;
        private readonly IHubHandler _handler;
        private readonly IOperatorProvider _operator;

        private readonly ILogger<MessageHub> _logger;

        //   private readonly IHubContext<MessageHub, IMessageHub> _hubContext;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="handler"></param>
        /// <param name="userMessageApp"></param>
        /// <param name="operator"></param>
        /// <param name="logger"></param>
        public MessageHub(IHubHandler handler, IUserMessageApp userMessageApp, IOperatorProvider @operator,
            ILogger<MessageHub> logger) //, IHubContext<MessageHub, IMessageHub> hubContext
        {
            _handler = handler;
            _userMessageApp = userMessageApp;
            _operator = @operator;
            _logger = logger;
            //  _hubContext = hubContext;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="message"></param>
        public async Task Send(HubMessage message)
        {
            await _handler.Send(Clients, message);
        }

        /// <summary>
        /// 
        /// </summary>
        public override async Task OnConnectedAsync()
        {
            var id = Context.ConnectionId;
            var userId = _operator.GetOperator().OperatorId;
            _logger.LogInformation($"客户端ConnectionId=>【{id}】已连接服务器！,userId:{userId}");
            await Groups.AddToGroupAsync(id, GROUP_NAME);
            await _userMessageApp.CreateClient(Context.ConnectionId, userId);
          
            await base.OnConnectedAsync();
            
            await OnLineUserList(userId, _operator.GetOperator().OperatorName, true);
        }
        /// <summary>
        /// 
        /// </summary>
        public async Task OnLineUserList(string id, string name, bool isOnLine)
        {
            await Clients.Groups(GROUP_NAME).SendAsync("OnlineUserList", new
            {
                RealName = name,
                Online = true
            });
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="exception"></param>
        public override async Task OnDisconnectedAsync(Exception exception)
        {
            var id = Context.ConnectionId;
            var userId = _operator.GetOperator().OperatorId;
            _logger.LogInformation($"客户端ConnectionId=>【{id}】已断开服务器连接！,userId:{userId}");
            await _userMessageApp.RemoveClient(userId, id);
            await base.OnDisconnectedAsync(exception);
            await OnLineUserList(userId, _operator.GetOperator().OperatorName, false);
        }
    }
}